package dao.impl;

import bean.merch;
import bean.supplier;
import bean.wareHouse;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import dao.merchDao;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import utils.c3p0Utils;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class merchDaoImpl implements merchDao {

    @Override
    public int merchAdd(int merchType, String merchName, Date merchDate, double merchPrice) {
        String sql="insert into Merch (mertypeId,merchname,merchdate,merchprice) VALUES (?,?,?,?)";
        QueryRunner mer=new QueryRunner(c3p0Utils.getDataSource());
        int i=0;
        try {
            i=mer.update(sql,merchType,merchName,merchDate,merchPrice);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        System.out.println(i);
        return i;
    }

    @Override
    public int merchUpdate(int merchId, int merchType, String merchName, Date merchDate, double merchPrice) {
        QueryRunner mer=new QueryRunner(c3p0Utils.getDataSource());
        String sql = "UPDATE Merch SET mertypeId=?,merchname=?,merchdate=?,merchprice=? WHERE merchId=?";
        int i=0;
        try {
            i=mer.update(sql,merchType,merchName,merchDate,merchPrice,merchId);//
        } catch (SQLException e) {
            e.printStackTrace();
        }
        System.out.println(i);
        return i;
    }

    @Override
    public int merchDel(int merchId) {
        QueryRunner mer=new QueryRunner(c3p0Utils.getDataSource());
        int i=0;
        try {
            i=mer.update("delete from Merch where merchId = ?",merchId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }

    @Override
    public PageInfo meList(int page, int size) {
        QueryRunner mer=new QueryRunner(c3p0Utils.getDataSource());
        PageHelper.startPage(page,size);
        List<merch> list=null;
        String sql = "select * from Merch";
        try {
            list = mer.query(sql, new BeanListHandler<merch>(merch.class));
            System.out.println(list);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        PageInfo<merch> pageinfo=new PageInfo<>(list);

        return pageinfo;
    }

    @Override
    public merch merchSingal(int merchId) {
        QueryRunner mer=new QueryRunner(c3p0Utils.getDataSource());
        merch me=null;
        try {
            me=mer.query("select * from Merch where merchId = ?",new BeanHandler<merch>(merch.class),merchId);//

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return me;
    }

    @Override
    public PageInfo merchOne(String merchname) {
        QueryRunner qr=new QueryRunner(c3p0Utils.getDataSource());
        List<merch> meOne= null;
        try {
            meOne=qr.query("select * from Merch where merchname=? ",new BeanListHandler<merch>(merch.class),merchname);//

        } catch (SQLException e) {
            e.printStackTrace();
        }
        PageInfo<merch> pageinfo=new PageInfo<>(meOne);
        return pageinfo;
    }

    @Override
    public List<merch> merchList() {
        QueryRunner mer=new QueryRunner(c3p0Utils.getDataSource());
        List<merch> list=null;
        String sql = "select * from Merch";
        try {
            list = mer.query(sql, new BeanListHandler<merch>(merch.class));
            System.out.println(list);
        } catch (SQLException e) {
            e.printStackTrace();
        }


        return list;
    }

}
